<?php

class Mount_DOM_tree {

   function tab($quant) {
      $result = "";
      for ($i = 0; $i < $quant; $i++) {
         $result .= "&nbsp;";
      }
      return $result;
   }

   //monta o código html
   function structHTML($f, $param, $tab) {
      if ($f->nodeName != "#text") {
         $atributos_t = "";
         $cont_att = 0;
         foreach ($f->attributes as $ob) {
            if ($cont_att >= 2) {
               $atributos_t .= "<br/>" . $this->tab($tab) . "&nbsp;";
               $cont_att = 0;
            }
            $atributos_t .= ' ' . $ob->name . '="' . $ob->value . '"';
            $cont_att++;
         }

         $result = $this->tab($tab) . "&LT;$f->nodeName$atributos_t&GT;";

         if ($f->childNodes->length > 0) {
            if ($param > 0) {
               $result .= "<br>";
            } else {
               $result .= "...";
            }
         }

         if ($param > 0 && $f->childNodes != null) {
            foreach ($f->childNodes as $nodes) {
               $result .= $this->structHTML($nodes, $param - 1, $tab + 3);
            }
         }

         $result .= (($param > 0 && $f->childNodes->length > 0) ? "" . $this->tab($tab) . "" : '') . "&LT;/$f->nodeName&GT; <br>";
      } else {
         return ( strlen(trim($f->wholeText)) > 0 ? "" . $this->tab($tab) . $f->wholeText . "<br>" : "" );
      }
      return $result;
   }

   
   
   /**
    * Função para o primeiro elemento da árvore DOM
    * @param type $f nodo do documento HTML (nodo)
    * @param type $param Quantidade de filhos que vai percorrer (1)
    * @param type $tab Inicio da tabulação (0)
    * @param type $new Novo elemento, caso aja substituição (Header)
    * @param type $Attributes mostra atributos (1) Não mostra atributos (0)
    * @param type $addAtt Adicionar algum atributo
    * @param type $removeAtt Remover os atributos
    * @return string Arquivo HTML
    */
   function structHTMLElementRefactory($f, $param, $tab, $new, $Attributes = 1, $addAtt = "", $removeAtt = "") {
      if ($f->nodeName != "#text") {
         $atributos_t = "";

         if ($Attributes) {
            foreach ($f->attributes as $ob) {
               if (!in_array($ob->name, $removeAtt)) {
                  $atributos_t .= ' ' . $ob->name . '="' . $ob->value . '"';
               }
            }
         }

         $atributos_t .= $addAtt;

         $result = $this->tab($tab) . "&LT;$new$atributos_t&GT;";

         if ($f->childNodes->length > 0) {
            if ($param > 0) {
               $result .= "<br>";
            } else {
               $result .= "...";
            }
         }

         if ($param > 0 && $f->childNodes != null) {
            foreach ($f->childNodes as $nodes) {
               $result .= $this->structHTML($nodes, $param - 1, $tab + 3);
            }
         }

         $result .= ($param > 0 && $f->childNodes->length > 0 ? "" . $this->tab($tab) . "" : '') . "&LT;/$new&GT;<br>";
      } else {
         return ( strlen(trim($f->wholeText)) > 0 ? "" . $this->tab($tab) . $f->wholeText . "<br>" : "" );
      }
      return $result;
   }

}
?>

